package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Date;

import javax.swing.JOptionPane;

import CheckInputs.CheckInputs;
import Entity.ContactCustomer;
import Entity.Product;
import Entity.Sale;
import GUI.ManagerEmployee.ManagerEmployeeGUI;
import GUI.ManagerEmployee.NewSalePnl;
import Message.Message;
import S2.client.S2Client;
import GUI.ManagerEmployee.*;


public class ManagerEmpCotntroler implements java.awt.event.ActionListener{
	
	private static final ActionListener ManagerEmpCotntroler = null;
	private ManagerEmployeeGUI ManEmpGui;
	private static GUI.ManagerEmployee.NewSalePnl addSalePnl;
	static  S2Client clientConection;
	
	public ManagerEmpCotntroler(){
		if(clientConection == null)
		clientConection = MainControler.getInstance();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		//Switch by the relevant panel.
		boolean checkInput;
		switch (e.getActionCommand()) {
		
//===========================  Add Sale ============================ 		
		
		
		case "Add Sale":
			String saleId;
			String saleName;
			String saleSDate;
			String saleEDate;
			Product saleProduct = new Product();
			
			saleSDate = ManEmpGui.newSalePnl.textFieldSDate.getText();
			saleEDate = ManEmpGui.newSalePnl.textFieldEDate.getText();
			saleName = ManEmpGui.newSalePnl.textFieldName.getText();
			saleId = ManEmpGui.newSalePnl.textFieldId.getText();
			
			if(
					!(checkInput = CheckInputs.CheckIfNumeirc(saleId))
					|| !(checkInput = CheckInputs.CheckIfNoNumeirc(saleName))
					|| !(checkInput = CheckInputs.CheckIsValidDate(saleSDate))
					|| !(checkInput = CheckInputs.CheckIsValidDate(saleEDate))
				){
						JOptionPane.showMessageDialog(null, "Invaild Input!", "InfoBox: " + "AddCustomer", JOptionPane.INFORMATION_MESSAGE);
						clearAddSalePnl();						
						break;
					}
			
			Sale sale = new Sale(saleId,saleName,saleSDate,saleEDate);
			
			Message addsalemsg = new Message(sale,"Add Sale");
			
			try {
				clientConection.sendToServer(addsalemsg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			clearAddSalePnl();
			break;
			


			
//=================================  Generate Report ======================================
			
			case "Generate Report":
				Message generatReport = new Message(null,"Generate Report");
				
				try {
					clientConection.sendToServer(generatReport);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			
			break;
			
			

//=================================  Get Responses ======================================			
			
			
		case "Get Responses":
			String repostSaleId;

			repostSaleId = ManEmpGui.responsesReportPnl.textFieldReportSaleId
					.getText();

			if (!(checkInput = CheckInputs.CheckIfNumeirc(repostSaleId))

			) {
				JOptionPane.showMessageDialog(null, "Invaild Input!",
						"InfoBox: " + "AddCustomer",
						JOptionPane.INFORMATION_MESSAGE);
				ManEmpGui.responsesReportPnl.textFieldReportSaleId.setText(" ");
				break;
			}
			Message getResponses = new Message(repostSaleId, "Get Responses");

			try {
				clientConection.sendToServer(getResponses);
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			ManEmpGui.responsesReportPnl.textFieldReportSaleId.setText(" ");
			break;
				
				
				
//=================================  Add Order ======================================				
		
		case "Add Order":
			
			String templateId;
			String templateName;
			String templateDesc;
			
			templateId = ManEmpGui.addSaleTemplatePnl.textFieldId.getText();
			templateName = ManEmpGui.addSaleTemplatePnl.textFieldName.getText();
			templateDesc = ManEmpGui.addSaleTemplatePnl.textFieldDesc.getText();
			
			if(
					!(checkInput = CheckInputs.CheckIfNumeirc(templateId))
					|| !(checkInput = CheckInputs.CheckIfNoNumeirc(templateName))
				){
						JOptionPane.showMessageDialog(null, "Invaild Input!", "InfoBox: " + "AddCustomer", JOptionPane.INFORMATION_MESSAGE);
						clearAddOrderPnl();					
						break;
					}
			
		//	System.out.print(templateId +"\n"+templateName +"\n"+ templateDesc );			
			clearAddOrderPnl();			
			break;
			

		default:
			System.out.println("btata");
			break;
		}
		
		
	}

	

	public void setView(ManagerEmployeeGUI ManEmpGui) {
		this.ManEmpGui = ManEmpGui;
	}

	public static void addSaleReturnMsg(Message msg) {
		
		
	}

	public static void generateReport(Message msg) {
		ManagerEmployeeGUI managerGui = new ManagerEmployeeGUI();
		
		managerGui.customerReportPnl.textPane_1.setText(msg.getStrReturn());
		
		
	}

	public static void getResponses(Message msg) {
		
		
	}
	
	private void clearAddOrderPnl() {
		ManEmpGui.addSaleTemplatePnl.textFieldId.setText("");
		ManEmpGui.addSaleTemplatePnl.textFieldName.setText("");
		ManEmpGui.addSaleTemplatePnl.textFieldDesc.setText("");
		
	}

	private void clearAddSalePnl() {
		ManEmpGui.newSalePnl.textFieldId.setText(" ");
		ManEmpGui.newSalePnl.textFieldName.setText(" ");
		ManEmpGui.newSalePnl.textFieldSDate.setText(" ");
		ManEmpGui.newSalePnl.textFieldEDate.setText(" ");
		
	}
}
